package io.modifier.basic.compose.widget.comm

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.modifier.basic.compose.animation.LoadingSpinner
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.theme.appColor
import io.modifier.basic.compose.theme.color
import io.modifier.basic.emo.ui.core.modifier.noRippleClickable
import io.modifier.basic.emo.ui.core.modifier.throttleClick

@Composable
fun RowScope.H(num: Number) {
    Box(modifier = Modifier.width(num.toDouble().dp))
}

@Composable
fun RowScope.HS() {
    Box(modifier = Modifier.weight(1f))
}

@Composable
fun ColumnScope.V(num: Number) {
    Box(modifier = Modifier.height(num.toDouble().dp))
}

@Composable
fun ColumnScope.VS() {
    Box(modifier = Modifier.weight(1f))
}

@Composable
fun CenterBox(modifier: Modifier = Modifier, content: @Composable BoxScope.() -> Unit) {
    Box(modifier, contentAlignment = Alignment.Center, content = content)
}

@Composable
fun CenterRow(
    modifier: Modifier = Modifier,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Center,
    content: @Composable RowScope.() -> Unit
) {
    Row(modifier, verticalAlignment = Alignment.CenterVertically, horizontalArrangement = horizontalArrangement, content = content)
}

@Composable
fun CenterColumn(
    modifier: Modifier = Modifier,
    verticalArrangement: Arrangement.Vertical = Arrangement.Center,
    content: @Composable ColumnScope.() -> Unit
) {
    Column(modifier, horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = verticalArrangement, content = content)
}

@Composable
fun SpaceColumn(modifier: Modifier = Modifier, space: Dp = AppThemeHolder.config.gap, content: @Composable ColumnScope.() -> Unit) {
    Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(space)) { content() }
}

@Composable
fun ScrollColumn(modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit) {
    Column(modifier = modifier.verticalScroll(rememberScrollState())) { content() }
}


@Composable
fun ThisDivider(modifier: Modifier = Modifier) {
    Box(modifier = modifier.background(AppThemeHolder.colors.dividerColor))
}

@Composable
fun ImgRes(icon: Int, modifier: Modifier = Modifier, contentScale: ContentScale = ContentScale.Fit) {
    Image(modifier = modifier, painter = painterResource(icon), contentDescription = "", contentScale = contentScale)
}

@Composable
fun ImgRes(
    icon: Int,
    sizeInt: Int = 24,
    contentScale: ContentScale = ContentScale.Fit,
    onClick: (() -> Unit)? = null
) {
    if (icon == 0) return
    if (sizeInt == 0) return
    ImgRes(
        icon, modifier = Modifier
            .size(sizeInt.dp)
            .noRippleClickable(enabled = onClick != null) { onClick?.invoke() }, contentScale = contentScale
    )
}

@Composable
fun AppColorBtn(
    nameStr: String,
    cor: RoundedCornerShape = AppThemeHolder.config.round,
    paddingDp: Dp = AppThemeHolder.config.gap,
    ts: TextStyle = AppThemeHolder.textStyle.title,
    onClick: () -> Unit
) {
    Box(
        modifier = Modifier
            .padding(paddingDp)
            .fillMaxWidth()
            .height(45.dp)
            .background(
                AppThemeHolder.colors.appColor,
                cor,
            )
            .clip(cor)
            .throttleClick {
                onClick.invoke()
            },
        contentAlignment = Alignment.Center,
    ) {
        Text(text = nameStr, style = ts.color(AppThemeHolder.colors.onAppColor))
    }

}

@Composable
fun LoadingPage() {
    Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
        LoadingSpinner()
    }
}

@Composable
fun EmptyPage(
    bgColor: Color = Color.Transparent,
    text: String = "这里空空的",
    textStyle: TextStyle = AppThemeHolder.textStyle.title.appColor(),
    iconRes: Int = 0,
    iconSize: Int = 0,
    content: (@Composable ColumnScope.() -> Unit)? = null,
    onClick: () -> Unit = {}
) {
    Column(
        Modifier
            .fillMaxSize()
            .background(bgColor)
            .noRippleClickable { onClick.invoke() },
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {

        content?.invoke(this) ?: run {
            if (iconRes != 0) {
                ImgRes(iconRes, iconSize)
                V(10)
            }
            Text(text, style = textStyle)
        }

    }
}